/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  Date: 23/Jan/2014
//  E-Mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//  Application: ADC Function
//  ADC Channel 0(P0.1 default) as ADC input
//
//  Output : UART show ADC output result on hyper-terminal
//***********************************************************************************************************

//------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
// <h> UART pin Select
//     <o0.6> UART pin
//          <0=> Select P1.0, P1.1 as UART pin(default)
//          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
// </h>
//-------------------------------- <<< end of configuration section >>> -------------------------------------
#define ADC_C_
#define Uart_Port_Sel   0x00

#include "N79E81x.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "ADC.h"
#include "Version.h"
#include <intrins.h>
#include <stdio.h>
#include <string.h>

//-----------------------------------------------------------------------------------------------------------
void Enable_ADC_Interrupt(void)
{
    EADC = 1;
    EA = 1;
}
//-----------------------------------------------------------------------------------------------------------
void ADC_Channel_Sel(E_ADCCNL_SEL channel)
{
    switch (channel)
    {
        case E_CHANNEL0:                                // P0.1 (default)
            clr_AADR2;
            clr_AADR1;
            clr_AADR0;
            break;
        case E_CHANNEL1:                                // P0.2
            clr_AADR2;
            clr_AADR1;
            set_AADR0;
            break;
        case E_CHANNEL2:                                // P0.3
            clr_AADR2;
            set_AADR1;
            clr_AADR0;
            break;
        case E_CHANNEL3:                                // P0.4
            clr_AADR2;
            set_AADR1;
            set_AADR0;
            break;
        case E_CHANNEL4:                                // P0.5
            set_AADR2;
            clr_AADR1;
            clr_AADR0;
            break;
        case E_CHANNEL5:                                // P0.6
            set_AADR2;
            clr_AADR1;
            set_AADR0;
            break;
        case E_CHANNEL6:                                // P0.7
            set_AADR2;
            set_AADR1;
            clr_AADR0;
            break;
        case E_CHANNEL7:                                // P2.6
            set_AADR2;
            set_AADR1;
            set_AADR0;
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Set_ADC_Input_Mode(E_ADCCNL_SEL channel)
{
    switch (channel)
    {
        case E_CHANNEL0:                                // ADC0(P0.1) is input-only mode
            P0DIDS |= SET_BIT1;                         // Disable digital function for P0.1
            P0M1 |= SET_BIT1;
            P0M2 &= CLR_BIT1;
            break;
        case E_CHANNEL1:                                // ADC1(P0.2) is input-only mode
            P0DIDS |= SET_BIT2;                         // Disable digital function for P0.2
            P0M1 |= SET_BIT2;
            P0M2 &= CLR_BIT2;
            break;
        case E_CHANNEL2:                                // ADC2(P0.3) is input-only mode
            P0DIDS |= SET_BIT3;                         // Disable digital function for P0.3
           P0M1 |= SET_BIT3;
            P0M2 &= CLR_BIT3;
			//	P0DIDS |= SET_BIT3;                         // Disable digital function for P0.3
          //  P0M1 = SET_BIT3;
          //  P0M2 = 0x00;
            break;
        case E_CHANNEL3:                                // ADC3(P0.4) is input-only mode
            P0DIDS |= SET_BIT4;                         // Disable digital function for P0.4
            P0M1 |= SET_BIT4;
            P0M2 &= CLR_BIT4;
            break;
        case E_CHANNEL4:                                // ADC4(P0.5) is input-only mode
            P0DIDS |= SET_BIT5;                         // Disable digital function for P0.5
            P0M1 |= SET_BIT5;
            P0M2 &= CLR_BIT5;
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Trigger_ADC_Convertion(void)
{
    clr_ADCI;                                           // Clear ADC flag (ADCI=0)
    set_ADCS;                                           // ADC run (ADCS = 1)
    PCON |= SET_BIT0;                                   // Enter idle mode
}
//-----------------------------------------------------------------------------------------------------------
void ADC_Init(void)
{
    Set_ADC_Input_Mode(E_CHANNEL0);                     // Set ADC0 (P0.1 default) is input only mode
   Set_ADC_Input_Mode(E_CHANNEL1);  


   // Enable_ADC_Interrupt();
	 EADC = 1;
    set_ADCEN;                                          // Enable ADC Function
}
//-----------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------
void ADC_ISR(void) interrupt 11                         // Vector @  0x5B
{
    clr_ADCI;                                           // Clear ADC flag (ADCI = 0)
    clr_ADCS;                                           // ADC stop (ADCS = 0)
}
//-----------------------------------------------------------------------------------------------------------



#undef ADC_C_


